#!/usr/bin/env python
# coding=utf8

#
# 發送聚會聚會手記
#
# Author: Chun-Yu Lee (Mat) <matlinuxer2@gmail.com>
# Copyright: Chun-Yu Lee (Mat) <matlinuxer2@gmail.com>
# License: MIT
#

import common
import sys
from xmlrpclib import ServerProxy
from Gmail import Gmail

common.read_settings_from_file()

PartyDate = common.thisThursday()

if sys.argv.__len__() == 2:
    PartyDate = sys.argv[1]

if common.isThursday(PartyDate) is False:
    print "Error: ", PartyDate, " is not Thursday..."
    exit()

if common.isFuture(PartyDate) is True:
    print "Error: ", PartyDate, " is in the future..."
    exit()

api_user          = common.volatile_settings['wikidot_api_user']
api_key           = common.volatile_settings['wikidot_api_key']
username          = common.volatile_settings['username']
password          = common.volatile_settings['password']
Sender            = common.volatile_settings['who']  # 值日生的 email
Reciver           = common.volatile_settings['email_address']
Subject           = "HackingThursday 聚會手記 (" + PartyDate + ")"
WikidotNoteURL    = "http://www.hackingthursday.org/" + PartyDate
EtherpadURL       = "http://sync.in/ep/pad/export/h4/latest?format=txt"
lastweek          = common.prevThursday(PartyDate)
nextweek          = common.nextThursday(PartyDate)

MailContentHeader = common.mail_content_header
MailContentFooter = common.mail_content_footer


s                 = ServerProxy('https://' + api_user + ':' + api_key + '@www.wikidot.com/xml-rpc-api.php')

##
## 先改成手動移動內容，順便 review 一下，以免誤發不合適的內容
##
## ################################################################################
## # 抓取 etherpad 上的內容，然後自動更新到對應的 wikidot 頁面
## ################################################################################
## the_content_txt   = common.get_etherpad_content_body( EtherpadURL )
##
## if the_content_txt == None:
## 	print "抓取 Etherpad 內容失敗"
## 	exit(1)
##
## #the_content_html  = common.get_wikidot_content_body( WikidotNoteURL )
##
## s = ServerProxy('https://'+api_user+':'+api_key+'@www.wikidot.com/xml-rpc-api.php')
##
## print the_content_txt
##
## site    = 'hackingthursday'
## page    = PartyDate
## title   = PartyDate + ' 聚會手記'
## content = '''
## [[[ %s |上一週]]] || [[[ %s |下一週]]]
##
## [[toc]]
##
## %s
##
## ''' % (lastweek, nextweek, the_content_txt )
##
## s.pages.save_one({'site' : site, 'page' : page, 'title' : title, 'content' : content})
##
## print 'To view : http://www.hackingthursday.org/%s' % ( page )


################################################################################
# 更新主選單的選單列表
################################################################################
site = "hackingthursday"
page2 = "nav:top"
target_keyword = u"\n* [# 聚會手記] \n"

nav_top_page = s.pages.get_one({"site": site, "page": page2})
nav_top_content = nav_top_page["content"]

foundReturn = nav_top_content.find(PartyDate)
if foundReturn < 0:
    k_start = nav_top_content.find(target_keyword)
    if k_start >= 0:
        k_end = k_start + target_keyword.__len__()

        new_nav_top_content = nav_top_content[0:k_end] + u" * [[[" + PartyDate + "]]]\n" + nav_top_content[k_end:]

        #print new_nav_top_content
        s.pages.save_one({'site': site, 'page': page2, 'content': new_nav_top_content})
else:
    print "頁面的對應選單項目已建立"

print 'To view : http://www.hackingthursday.org/%s' % (page2)

################################################################################
# 寄通知信
################################################################################
Html = """
<html>
    <head>
        <title>HackingThursday</title>
    </head>
    <body>
    <pre>""" + MailContentHeader + """</pre>
    """ + WikidotNoteURL + """
    <pre>""" + MailContentFooter + """</pre>
    </body>
</html>
"""

Txt = common.html2txt(Html)

if __name__ == "__main__":
    receivers = Reciver.split(',')
    for item in receivers:
        receiver = item.strip()

        gmail = Gmail(username, password)
        gmail.send(Sender, receiver, Subject, Txt, Html)
